<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
//function deletetank($tankid,$string){
//	
//	}   
function showlvl($lvl){
	if ($lvl == 10){
		echo "X";
  } else if ($lvl == 9){
      	  	  	echo "IX ";
      	  	  } else if ($lvl == 8){
      	  	  	echo "VIII ";
      	  	  } else if ($lvl == 7){
      	  	  	echo "VII ";
      	  	  } else if ($lvl == 6){
      	  	  	echo "VI ";
      	  	  } else if ($lvl == 5){
      	  	  	echo "V ";
      	  	  } else if ($lvl == 4){
      	  	  	echo "IV ";
      	  	  } else if ($lvl == 3){
      	  	  	echo "III ";
      	  	  } else if ($lvl == 2){
      	  	  	echo "II ";
      	  	  } else if ($lvl == 1){
      	  	  	echo "I ";
      	  	  } else {
      	  	  	echo " ";
      	  	  }
	
	}
function toptanks(){
	$db =& JFactory::getDbo();	
	$query = $db->getQuery(true);
	$query="SELECT * FROM `#__wotclan_tanks` WHERE `lvl`='10'";
	$db->setQuery($query);
	$db->query();
	$num_rows = $db->getNumRows();	
	$rows = $db->loadObjectList();
	foreach ($rows as $row) {
		if ($list==""){
			$list=$row->uniqid;	
		} else {
 			$list=$list."|".$row->uniqid;	
 		}
 	}
 	//echo "<h1>list:$list</h1>";
 	return $list;
} 
function showimgtank($tankname,$cname,$type,$uniqid){
	$tabletank=explode(':',$tankname);
      		    //$tabletank[0]=$row2->name);
      		    	if ($tabletank[0] == "france"){
      		    		echo "<img src='/media/wotclan/flags/fr.gif'> ";
      		    	}	else if ($tabletank[0] == "germany"){
      		    		echo "<img src='/media/wotclan/flags/de.gif'> ";
      		    	}	else if ($tabletank[0] == "ussr"){
      		    		echo "<img src='/media/wotclan/flags/ru.gif'> ";
      		    	}	else if ($tabletank[0] == "china"){
      		    		echo "<img src='/media/wotclan/flags/tr.gif'> ";
      		    	}	else if ($tabletank[0] == "uk"){
      		    		echo "<img src='/media/wotclan/flags/en.gif'> ";
      		    	}	else if ($tabletank[0] == "usa"){
      		    		echo "<img src='/media/wotclan/flags/us.gif'> ";
      		    	}	else {
      		    		echo $tabletank[0]." "; 
      		    	}
      		    	if ($type == 1){
      		    		echo "<img height='16' src='/media/wotclan/lourd.png'> ";
      		    	} else if ($type == 2){
      		    		echo "<img  height='16' src='/media/wotclan/medium.png'> ";
      		    	} else if ($type == 3){
      		    		echo "<img height='16' src='/media/wotclan/light.png'> ";
      		    	} else if ($type == 4){
      		    		echo "<img height='16' src='/media/wotclan/td.png'> ";
      		    	} else if ($type == 5){
      		    		echo "<img height='16' src='/media/wotclan/arty.png'> ";
      		    	}
      		  		if ($cname == ""){
                        if (uniqid == ""){
                            echo $tabletank[1];
                        } else {
                            echo '<a href="index.php/component/wotclan/component/wotclan/?view=show&type=tank&id=',$uniqid,'">',$tabletank[1],'</a>';
                        }
      			  	} else {

                        if (uniqid == ""){
      			  		    echo $cname;
                        } else {
                            echo '<a href="index.php/component/wotclan/component/wotclan/?view=show&type=tank&id=',$uniqid,'">',$cname,'</a>';
                        }
      			  	}
    			    
    			    
	}
function replacetier($account){
	//echo 'in replace tier '.$account;
	$db =& JFactory::getDbo();	
	$query3 = $db->getQuery(true);
	
	$query3="SELECT * FROM `#__wotclan_account` WHERE `uniqid`='".$account."'";
	$db->setQuery($query3);
	$db->query();
	$num_rows3 = $db->getNumRows();
	//echo 'numroes='.$num_rows3;
	$rows3 = $db->loadObjectList();
	$tankstring='';
	$tier=10;
	//$types=[5]; ajouter un ordre dans les type (ex: arty, puis lourd, puis td, puis medium, etc)
	while ($tier>0){

	  foreach ( $rows3 as $row3 ) {
		$tank = explode("|",$row3->tanks);
		$x=0;
		//echo '<br>tanks='.$tank[$x];

		while ($tank[$x] != ""){
    			$query4 = $db->getQuery(true);
    			$query4="SELECT * FROM `#__wotclan_tanks` WHERE `uniqid`='".$tank[$x]."'";
    			$db->setQuery($query4);
      			$db->query();
      			$num_rows4 = $db->getNumRows();
      			$rows4 = $db->loadObjectList();
       
      			if ($num_rows4 == 1){


      				foreach ( $rows4 as $row4 ) {
					if ($row4->lvl==$tier){
						if ($tankstring==""){
							$tankstring=$row4->uniqid;
						}else {
							$tankstring=$tankstring.'|'.$row4->uniqid;
						}

        				}
    			    
                                 }
                
        } elseif ($num_rows4 == 0) {
      	echo "<tr>Prob avec le tank ".$x."(".$tank[$x].") : aucun tank</tr>";
        } else {
      	echo "<tr>Prob avec le tank ".$x."(".$tank[$x].") : trop de tank</tr>";
        }
        
        
			$x++;
	}
  	}
		$tier=$tier-1;
	}
	$query="UPDATE `#__wotclan_account` SET `tanks`='".$tankstring."' WHERE uniqid='".$account."';";
        $db->setQuery($query);
        //echo $query;
        $db->query();
}
function analyse_team($line,$file){
	jimport( 'joomla.database.database' );
	$document	= JFactory::getDocument();
//	JHTML::_('stylesheet', 'style.css', 'components/com_icagenda/add/css/');
	$document->addStyleSheet( JURI::base().'components/com_wotclan/css/style.css' );
 $user =& JFactory::getUser();
 $usr_id = $user->get('id');
   
 $db =& JFactory::getDbo();									 

 //echo "<h1>".$line."</h1>";
 // HERE
 if (preg_match("/.*frags.*/", $line)){
 	$matchfini=true;
 }
 $mapname=preg_replace('/.*"mapName": "([a-zA-Z0-9_:\-]*)".*/','$1',$line);
 $mapname=preg_replace('/\s/','',$mapname);
// echo "<br>here the map : " . $mapname . "<br>";
 $mapDisplayName=preg_replace('/.*"mapDisplayName": "([a-zA-Z0-9_:\-\s]*)".*/','$1',$line);
 $query = $db->getQuery(true);
 $query="SELECT * FROM `#__wotclan_maps` WHERE name='".$mapDisplayName."'";
 $db->setQuery($query);
 $db->query();
 $num_rows = $db->getNumRows();
 if($num_rows>1){
    echo "<br><h1>Il y a un probleme avec la map ".$mapDisplayName.": il y a plus d'un resultat</h1><br>";
	$rows = $db->loadObjectList();
  	foreach ( $rows as $row ) {
        //echo "<br>test : ".$row->name."<br>";
  	}
 } elseif ($num_rows>0){
    //echo "<br>Map  ".$mapDisplayName." deja present <br>";
	if ($mapname != $rows->wotname){
	    $query2 = $db->getQuery(true);
		$query2="UPDATE `#__wotclanmaps` SET `wotname`='".$mapname."' WHERE name='".$mapDisplayName."';";
		//echo "query for changing wotname map : ".$query2;
		$db->setQuery($query2);
        $db->query();
	}
 } else {
  	$mapuniq=uniqid("", true);
	$query2 = "INSERT INTO `#__wotclan_maps` (`id`,`uniqid`, `name`,`wotname`)
        VALUES (NULL,'".$mapuniq."',  '".$mapDisplayName."','".$mapname."');";
    	//echo "<br>".$query2."<br>";
	$db->setQuery( $query2 );
	$db->query();   
	$query3 = "INSERT INTO `#__wotclan_logs` (`id`,`date`, `text`)
        VALUES (NULL,now(), '".$mapDisplayName." - ".$mapname." a ete ajouter');";
    	//echo "<br>".$query2."<br>";
	$db->setQuery( $query3 );
	$db->query();   

 }
     		


 //echo "<br>here the mapdisplayname : " . $mapDisplayName . "<br>";
 $playerid=preg_replace('/.*"playerID": ([a-zA-Z0-9_:\-]*).*/','$1',$line);
 //$playerid=preg_replace('/\s/','',$playerid);
 //$playerid=preg_replace('/.*\{([a-zA-Z0-9_:\-]*)/','',$playerid);
 //echo "<br>here the PlayerID : " . $playerid . "<br>";
 $playerVehicle=preg_replace('/.*"playerVehicle": "([a-zA-Z0-9_:\-]*)".*/','$1',$line);
 //$playerVehicle=preg_replace('/[0-9]*\{/','',$playerVehicle);
// echo "<br>here the playerVehicule:" . $playerVehicle . "<br>";
 //$playerVehicle=preg_replace('/\s/','',$playerVehicle);
 // rechercher ce tank dans la bd et ajouter dans la bd battle
 $playerVehicle=preg_replace('/germany-/','germany:',$playerVehicle);
 $playerVehicle=preg_replace('/usa-/','usa:',$playerVehicle);
 $playerVehicle=preg_replace('/france-/','france:',$playerVehicle);
 $playerVehicle=preg_replace('/ussr-/','ussr:',$playerVehicle);
 $playerVehicle=preg_replace('/uk-/','uk:',$playerVehicle);
 $playerVehicle=preg_replace('/china-/','china:',$playerVehicle);
 $playerVehicle=preg_replace('/.*\{([a-zA-Z0-9_:\-]*).*/','$1',$playerVehicle);
 $playerVehicle=preg_replace('/\n/','',$playerVehicle);
  $test=preg_match('/.*"dateTime": "(\d{2})\.(\d{2})\.(\d{4}).* (\d{2}):(\d{2}):(\d{2}).*"}.*/',$line,$matches);
// echo "<br>dateTime : Jour : " . $matches[1] . " Mois :" . $matches[2] . " Annee :" . $matches[3] . " " . $matches[4] . ":" . $matches[5] . " " . $matches[6] . "<br>";
 $datetime=$matches[3]."-".$matches[2]."-".$matches[1]." ".$matches[4].":".$matches[5].":".$matches[6];
 //echo "<br>here the playerVehicle : " . $playerVehicle . "<br>";
 $query = $db->getQuery(true);
 $query="SELECT * FROM `#__wotclan_tanks` WHERE name='".$playerVehicle."'";
 $db->setQuery($query);
 $db->query();
 $num_rows = $db->getNumRows();
 if($num_rows>1){
    echo "Prob with ".$playerVehicle." on db, more then one result";
 } elseif ($num_rows == 0) {
    echo "Ce tank n'est pas dans la bd, mais le code pour l ajouter n est pas écris ...";
 } else {
                     
    $rows = $db->loadObjectList();
    $tankbattle = $rows[0]->uniqid ;
    
 } 


 $gameplayID=preg_replace('/".*gameplayID": "([a-zA-Z0-9_:\-]*)".*/','$1',$line);
 //$gameplayID=preg_replace('/\s/','',$gameplayID);
 $gameplayID=preg_replace('/.*\{([a-zA-Z0-9_:\-]*).*/','$1', $gameplayID);
 //echo "<br>here the gameplayID : " . $gameplayID . "<br>";
 $playerName=preg_replace('/".*playerName": "([a-zA-Z0-9_:\-]*)".*/','$1',$line);
 $playerName=preg_replace('/.*\{([a-zA-Z0-9_:\-]*).*/','$1', $playerName);
 $playerName=preg_replace('/\s/', '', $playerName);
 //echo "<br>here the playerName : " . $playerName . "<br>";
 $query = $db->getQuery(true);
 $query="SELECT * FROM `#__wotclan_account` WHERE name='".$playerName."'";
 $db->setQuery($query);
 $db->query();
 //echo "<h1>$query</h1>";
 $num_rows = $db->getNumRows();
 if($num_rows>1){
    echo "Prob with ".$playerName." on db, mor then one result";
 }	elseif ($num_rows>0) {
			
    $rows = $db->loadObjectList();
  	foreach ( $rows as $row ) {
        $uniqaccount=$row->uniqid;
    								
  	}
	//echo "<br>".$playerName." already exist in DB<br>";
										
 } else {
           //       if ($results['column'] == NULL) { 
                  	//echo "<br>No result = " .$results."<br>";
    $uniqaccount=uniqid("", true);
    $query2 = "INSERT INTO `#__wotclan_account` (`id`,`uniqid`, `name`,`playerid`,`uid`)
			VALUES (NULL,'".$uniqaccount."',  '".$playerName."','".$playerid."','".$usr_id."');";
   			//echo "<br>".$query2."<br>";
    $db->setQuery( $query2 );
    $db->query();   
                  
 }
 $query = $db->getQuery(true);
 $query="SELECT * FROM `#__wotclan_user` WHERE uid='".$usr_id."'";
 $db->setQuery($query);
 $db->query();
 $num_rows = $db->getNumRows();
 if($num_rows>1){
    echo "Prob with ".$usr_id." on db, more then one result";
 }	elseif ($num_rows>0) {
							
    $rows = $db->loadObjectList();
  	foreach ( $rows as $row ) {
    	 //	verify if account is on user db
        if (preg_match("/.*".$uniqaccount.".*/",$row->account)){
    	    //echo "<h1>Account already present</h1>";
        } else {
    	    echo "<h1>Adding account to db</h1>";
    		if ($row->account == "") {
    		    $newaccount=$uniqaccount;
    		} else {
    		    $newaccount=$row->account."|".$uniqaccount;
    		}
    		$query2 = $db->getQuery(true);
    		$query2="UPDATE `#__wotclan_user` SET `account`='".$newaccount."' WHERE uid='".$usr_id."';";
    		$db->setQuery($query2);
        	$db->query();													    								  	
    	}
  	}
 } else { 
     					
    $query2 = $db->getQuery(true);
    $query2 = "INSERT INTO `#__wotclan_user` (`id`,`uniqid`, `uid`,`account`)
    VALUES (NULL,'".uniqid("", true)."',  '".$usr_id."','".$uniqaccount."');";
    $db->setQuery($query2);
    $db->query();
    										
 } 
                   



 $teamstring=preg_replace('/.*"vehicles": {(.*)}}.*/', '$1',$line);
 preg_match_all('/"[0-9]*": \{"vehicleType": /', $teamstring,$result);
// echo "<h1>TEST:</h1>";
 //echo "<h1>".$teamident[0]."</h1>";
 //echo "<h1>TEST2:</h1>";
 //echo "<h1>".$teamident[1]."</h1>";
 //echo "<h1>".$teamstring."</h1>";
 //print_r($result);
 $x=0;
 $t1=0;
 $t2=0;
 foreach($result[0] as $string){
    $teamid[$x]=preg_replace('/"([0-9]*)":.*/','$1',$string);
    //echo "test <h1>$teamid[$x]</h1>";
    $st2search='/.*"'.$teamid[$x].'": \{"vehicleType": "([a-zA-Z0-9_:\-]*)", "isAlive": (true|false), "name": "([a-zA-Z0-9_:\-]*)", "igrType": ([0-9]), "clanAbbrev": "([a-zA-Z0-9_\-:]*|)", "team": (1|2).*/';//, "isAlive": ([true|false]), "name": "([a-ZA-Z0-9].*)".*/';//, "clanAbbrev": "([a-ZA-Z0-9_].*)", "team": ([1|2]), "events": {([a-ZA-Z0-9\-_].*)"}, "isTeamKiller": ([true|false])\},.*/';
    //, "events": {([a-zA-Z0-9:\-_].*)"}, "isTeamKiller": (true|false)\}
    //, "team": (1|2), "events": {([a-zA-Z0-9:\-_].*)"}, "isTeamKiller": (true|false)\},
    $test=preg_match_all($st2search,$teamstring,$oresult);
    $vehicule[$x]=$oresult[1][0];
   // echo "<h1>$x $vehicule[$x] $oresult[1][0]</h1>";
    $query = $db->getQuery(true);
    $query="SELECT * FROM `#__wotclan_tanks` WHERE name='".$vehicule[$x]."'";
		$db->setQuery($query);
    $sqlresult=$db->query();
    //echo "<br>result for searching a tanks : $result<br>";
    $num_rows = $db->getNumRows();
    $rows = $db->loadObjectList();
    if($num_rows>1){
	    echo "<br><h1>Il y a un probleme avec le vehicule  ".$vehicule[$x].": il y a plus d'un resultat</h1><br>";
    } elseif ($num_rows>0){
	    //echo "<br>Vehicule  ".$vehicule[$x]."(".$rows[0]->uniqid.") deja present <br>";
        $tankuniq=$rows[0]->uniqid;
    } else {
      $tankuniq=uniqid("", true);
      echo "<br>Vehicule  ".$vehicule[$x]."(".$tankuniq.") n exite pas dans la db<br>";
			$query2 = "INSERT INTO `#__wotclan_tanks` (`id`,`uniqid`, `name`)
    		VALUES (NULL,'".$tankuniq."',  '".$vehicule[$x]."');";
    	echo "<h1>".$query2."</h1>";
			$db->setQuery( $query2 );
			$db->query();   
  	}
		//echo "<h1>tankuniq=$tankuniq</h1>";
    $isalive[$x]=$oresult[2][0];
    $name[$x]=$oresult[3][0];
    $igrtype[$x]=$oresult[4][0]; 
    $clan[$x]=$oresult[5][0]; 
    $team[$x]=$oresult[6][0];
    $id[$x]=$teamid[$x];
    if($matchfini){
      $frag[$x]=preg_replace('/.*"'.$teamid[$x].'": \{"frags": ([0-9]*)\}.*/','$1',$line);
    //echo "<h1>$x $team[$x]  $name[$x] $clan[$x] $isalive[$x]  $frag[$x]</h1>";
    }
    $query = $db->getQuery(true);
    $query="SELECT * FROM `#__wotclan_account` WHERE name LIKE '".$name[$x]."'";
    $db->setQuery($query);
    $db->query(); 
   	$num_rows = $db->getNumRows();
   	$rows = $db->loadObjectList();
   	foreach ( $rows as $row ) {
    	//echo "<br>test : ".$row->name.": ".$row->uid."<br>";
  	}
	if($num_rows>1){
	    echo "<br><h1>Il y a un probleme avec le joueur ".$name[$x].": il y a plus d'un resultat</h1><br>";
		foreach ( $rows as $row ) {
    	    //echo "<br>test : ".$row->name.": ".$row->uid."<br>";
  		}
	} elseif ($num_rows>0){
								
	  //  echo "<br>Joueur ".$name[$x]." deja present <br>";
		// verifier si il a le bon clan 
		if ($row->clan != $clan[$x]){
		  //echo "<br>Le joueur ".$name[$x]." a changer de clan<br>";
			//echo "<br> rows->clan :".$row->clan."<br>clanabb : ". $clan[$x]."<br>";
			$query4 = $db->getQuery(true);
			$query4="SELECT * FROM `#__wotclan_ragots` WHERE `name`='".$name[$x]."' LIMIT 0,1"; // TOTO , REGARDER LE PLUS RECENT
			$db->setQuery($query4);
      $db->query();
      $num_rows4 = $db->getNumRows();
      $rows4 = $db->loadObjectList();
      //if ($num_rows4 > 0) {
      //	if ($rows4[0]->date)
      // TODO VERIFIER SI LE DERNIER RAGOT EST PLUS RECENT ...
      if ((empty($row->clan)) && ( isset($clan[$x]))){
	      $text=$datetime." : Le joueur ".$name[$x]." a rejoint le clan : [<a href=\"index.php/component/wotclan/?view=search&clan=".$clan[$x]."\">".$clan[$x]."</a>]";
      } else if ((isset($row->clan)) && (empty($clan[$x]))) {
	       $text=$datetime." : Le joueur ".$name[$x]." a quitt&#233; le clan [<a href=\"index.php/component/wotclan/?view=search&clan=".$row->clan."\">".$row->clan."</a>].";
      } else {
	      $text=$datetime." : Le joueur ".$name[$x]." a changer de clan : ancien clan [<a href=\"index.php/component/wotclan/?view=search&clan=".$row->clan."\">".$row->clan."</a>] vers le nouveau clan : [<a href=\"index.php/component/wotclan/?view=search&clan=".$clan[$x]."\">".$clan[$x]."</a>]";
      }
      echo "<br>$text<br>";
			$query3 = $db->getQuery(true);
			$query3="INSERT `#__wotclan_ragots` (`id`, `uniqid`, `name`,`date`,`text`)
    			VALUES (NULL,'".uniqid("", true)."',  '".$name[$x]."','".$datetime."',$text);";
      $db->setQuery($query3);
      $sqlresult=$db->query(); 
      //echo "RESULT FOR INSERTING RAGOT FOR USER $name[$x] : $result";
      $query2 = $db->getQuery(true);
			$query2="UPDATE `#__wotclan_account` SET `clan`='".$clan[$x]."' WHERE name='".$name[$x]."';";
			$db->setQuery($query2);
      $sqlresult=$db->query(); 
      //echo "RESULT FOR SETTING NEW CLAN FOR USER $name[$x] : $result";
		}
		//echo "<h1>tankuniq=$tankuniq</h1>";
    if (preg_match("/.*".$tankuniq.".*/", $row->tanks)){
		    //echo "<br>Le tank ".$vehicule[$x]."(".$tankuniq.") du joueur ".$name[$x]." est deja present dans la DB<br>";
		} else {
		// ajouter le tank 
		    //echo "<br>Le tank ".$vehicule[$x]."(".$tankuniq.") du joueur ".$name[$x]." n est pas present dans la DB<br>";
			if ($row->tanks == NULL){
			    $SEP="";
			} else {
			    $SEP="|";
			}
			$stringvehicule=$row->tanks.$SEP.$tankuniq;
			//echo "<br>new string pour vehicule : ".$stringvehicule."<br>";
			$query2 = $db->getQuery(true);
			$query2="UPDATE `#__wotclan_account` SET `tanks`='".$stringvehicule."' WHERE name='".$name[$x]."';";
			//echo "<br>Query for update vehicule : ".$query2."<br>";
			$db->setQuery($query2);
            $db->query();
		}
	} else {
  	    $query2 = "INSERT INTO `#__wotclan_account` (`id`,`uniqid`, `name`,`clan`,`tanks`)
    			VALUES (NULL,'".uniqid("", true)."',  '".$name[$x]."','".$clan[$x]."','".$tankuniq."');";
    	//echo "<br>".$query2."<br>";
		$db->setQuery( $query2 );
		$db->query();   
  	}
    //echo "<h1>".$teamid[$x]." : ". $vehicule[$x]."</h1>";
    //echo "<h1>".$st2search."</h1>";
    //print_r($oresult);
    //echo "<h1>test = ".$test."</h1><h1>".$oresult[1][0]." is alive : ".$oresult[2][0]." name = ".$oresult[3][0]." clan : ".$oresult[4][0]." team = ".$oresult[5][0]."</h1>";
    //echo "<h1>".$x." name = ".$name[$x]."</h1>";
    //echo "<h1>".$x." team = ".$team[$x]."</h1>";
    $y=0;
    $notused=true;
    while ($y < $x) {
    	if ($name[$x]==$name[$y]){$notused=false;}
    	$y += 1;
    }
    if ($notused){
    	if ($team[$x]==1){
    		$team1['name'][$t1]=$name[$x];
    		$team1['vehicule'][$t1]=$vehicule[$x];
    		$team1['clan'][$t1]=$clan[$x];
    		$team1['isalive'][$t1]=$isalive[$x];
    		$team1['id'][$t1]=$id[$x];
		if ($matchfini){
			$team1['frags'][$t1]=$frag[$x];
		}
    		if ($steam1 == ""){
    			$steam1=$name[$x];
    			$sttankt1=$vehicule[$x];
    			//echo "<h1>test : $sttankt1 = ".$sttankt1."<br>    			$x : $vehicule[$x]</h1>";
    			
    		} else {
    			$steam1=$steam1."|".$name[$x];
    	    $sttankt1=$sttankt1."|".$vehicule[$x];
    	
    	    //echo "<h1>test : $sttankt1 = ".$sttankt1."<br>    			$x : $vehicule[$x]</h1>";
    		}
        //echo "<h1>t1: ".$t1."</h1>";
    		$t1 += 1;	
    	} else {
    		$team2['name'][$t2]=$name[$x];
    		$team2['vehicule'][$t2]=$vehicule[$x];
    		$team2['clan'][$t2]=$clan[$x];
    		$team2['isalive'][$t2]=$isalive[$x];
    		$team2['id'][$t2]=$id[$x];
		if ($matchfini){
			$team2['frags'][$t2]=$frag[$x];
		}
    				  				
    		if ($steam2 == ""){
    			$steam2=$name[$x];
    			$sttankt2=$vehicule[$x];
    			//echo "<h1>test : $sttankt2 = ".$sttankt2."<br>    			$x : $vehicule[$x]</h1>";
    			  					
    		} else {
    			$steam2=$steam2."|".$name[$x];
    			$sttankt2=$sttankt2."|".$vehicule[$x];
    			//echo "<h1>test : $sttankt2 = ".$sttankt2."<br>    			$x : $vehicule[$x]</h1>";
    		}
        //echo "<h1>t2: ".$t2."</h1>";
    		$t2 += 1;	
    	}
    }
 $x++;
 }
	echo "<div align='center'><h1>Map : $mapname</h1>";
        //echo "<h1>test : ".$team1['vehicule'][0]."</h1>";
 echo "<table><tr><td width='250' class='battletable'>Team 1</td>";
 if ($matchfini){
	 echo "<td width='250' class='battletable'>Dead/Alive</td>";
	 echo "<td width='250' class='battletable'>Frags</td>";
 }
 echo "<td width='250' class='battletable'>Vehicule</td><td width='250' class='battletable'>Vs</td><td width='250' class='battletable'>Team 2</td>";
 if ($matchfini){
	 echo "<td width='250' class='battletable'>Dead/Alive</td>";
	 echo "<td width='250'class='battletable'>Frags</td>";
 }

	 echo "<td width='250' class='battletable'>Vehicule</td></tr>";
 $x=0;
 while (isset($team1['name'][$x]) or isset($team2['name'][$x])){
    echo "<tr><td width='250' class='battletable'><a href='?view=show&type=joueur&joueur=".$team1['name'][$x]."'>".$team1['name'][$x]."</a>";
    if ($team1['clan'][$x]!== ''){
        echo "<a href='?view=search&clan=".$team1['clan'][$x]."'>[".$team1['clan'][$x]."]</a>";
    }
    if ($matchfini){
    echo "</td><td width='250' class='battletable'>";
    if ($team1['isalive'][$x]=='true'){
	    echo "<div style='color:green'>O</div>";
    }else if ($team1['isalive'][$x]=='false'){
	    echo "<div style='color:red'>X</div>";
    }
    echo "</td><td width='250' class='battletable'>";
    echo $team1['frags'][$x];
    }
    echo "</td><td width='250' class='battletable'>";
    showimgtank($team1['vehicule'][$x],"","","");
    echo "</td><td width='250' class='battletable'></td>".
    "<td width='250' class='battletable'><a href='?view=show&type=joueur&joueur=".$team2['name'][$x]."'>".$team2['name'][$x]."</a>";
    if ($team2['clan'][$x]!== ''){
        echo "<a href='?view=search&clan=".$team2['clan'][$x]."'>[".$team2['clan'][$x]."]</a>";
 	}
    if ($matchfini){
    echo "</td><td width='250' class='battletable'>";
        if ($team2['isalive'][$x]=='true'){
	    echo "<div style='color:green'>O</div>";
	}else if ($team2['isalive'][$x]=='false'){
	    echo "<div style='color:red'>X</div>";
    }
       echo "</td><td width='250' class='battletable'>";
    echo $team2['frags'][$x];
   
    }
    echo "</td><td width='250' class='battletable'>";
    
    showimgtank($team2['vehicule'][$x],"","","");
    echo"</td></tr>";	
    $x++;
 }
 echo "</table>";
 echo "</div>";
 //add datetime

 //$dest = JPATH_ROOT.DS. "media" . DS . "wotclan" . DS . "replays" . DS . $_FILES["file_upload"]["name"];
 
 if ($_POST['notshare']||$_GET['notshare']) {
 	//echo "<h1>to be deleted : </h1>" . $dest;
  unlink($file);
  } else {
  	$query = $db->getQuery(true);
    $battleuniq=uniqid("", true);
  	$query="INSERT `#__wotclan_battle` (`id`,`uniqid`, `team1`,`tanks1`,`team2`,`tanks2`,`datebattle`,`tank`)
    	VALUES (NULL,'".$battleuniq."','".$steam1."','".$sttankt1."','".$steam2."','".$sttankt2."','".$datetime."','".$tankbattle."');";
    //echo "<h1>query = ".$query;
	  $db->setQuery($query);
    $db->query();
            
  	$query = $db->getQuery(true);
    $replayuniq=uniqid("", true);
    $path_parts = pathinfo($_FILES["file_upload"]["name"]);
    $query="INSERT `#__wotclan_replays` (`id`,`uniqid`, `file`, `uid`, `battle`, `datebattle`,`map`)
          
    VALUES (NULL,'".$replayuniq."','".$path_parts['filename'].".wotreplay','".$usr_id."','".$battleuniq."','".$datetime."','".$mapname."');";
  	$db->setQuery($query);
    $db->query();
    //echo "<h1>replays Query : ".$query."</h1>";
    echo "Ajouter un titre : <form name='title' action='?view=addtitle&replayid=".$replayuniq."' method='post' enctype='multipart/form-data'> <input type='text' name='title' id='title'><input type='submit' name='submit' value='Submit'></form>";
  }
 	
 


}
?>
